<script setup>
import { reactive } from "vue";
import router from "@/router";
import { ElMessage } from "element-plus";
import { ErrCode, getUserInfo, login } from "@/utils/http";
import user from "@/store/user";

const data = reactive({
  form: {
    username: "",
    password: ""
  }
});

const onSubmit = () => {
  login(data.form).then((resp) => {
    if (resp.success()) {
      getUserInfo().then((resp) => {
        if (resp.success()) {
          user.login = true;
          user.info = resp.data;
        }
      });
      router.push("/");
      return;
    }
    switch (resp.code) {
      case ErrCode.USERNAME_PASSWORD_INVALID:
        ElMessage({
          showClose: true,
          message: "账号名或密码错误",
          type: "error"
        });
        break;
      default:
        ElMessage({
          showClose: true,
          message: "系统开小差啦，请稍后重试",
          type: "error"
        });
    }

  });
};
</script>
<template>
  <el-form :model="data.form" label-width="120px">
    <el-form-item label="用户名：">
      <el-input
        v-model="data.form.username"
        placeholder="用户名/邮箱/手机号"
        type="text"
      />
    </el-form-item>
    <el-form-item label="密码：">
      <el-input v-model="data.form.password" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">登陆</el-button>
    </el-form-item>
  </el-form>
</template>
